package response

import (
	"encoding/json"
	"fmt"
	"net/http"
)

// ResponseOk 正常响应
func ResponseOk(w http.ResponseWriter) {
	resultData := &Response{
		Code:    200,
		Message: "success",
		Data:    nil,
	}

	// Parse JSON
	result, err := json.Marshal(resultData)
	if err != nil {
		http.Error(w, "Failed to parse JSON", http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
	_, err = w.Write(result)
	if err != nil {
		fmt.Println("write-err:", err)
		return
	}
}

// ResponseFailMessage 失败响应
func ResponseFailMessage(w http.ResponseWriter, message string) {
	resultData := &Response{
		Code:    400,
		Message: message,
		Data:    nil,
	}

	// Parse JSON
	result, err := json.Marshal(resultData)
	if err != nil {
		http.Error(w, "Failed to parse JSON", http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
	_, err = w.Write(result)
	if err != nil {
		fmt.Println("write-err:", err)
		return
	}
}

// ResponseOkData 正常响应
func ResponseOkData(w http.ResponseWriter, code int, message string, data interface{}) {
	resultData := &Response{
		Code:    code,
		Message: message,
		Data:    data,
	}

	// Parse JSON
	result, err := json.Marshal(resultData)
	if err != nil {
		http.Error(w, "Failed to parse JSON", http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
	_, err = w.Write(result)
	if err != nil {
		fmt.Println("write-err:", err)
		return
	}

}
